/** All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the <ORGANIZATION> nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE. 
*/

#include "plant.h"
#include "global.h"
#include "valueparser.h"
#include <cstring>

/**  This file contains the default behavior for plant functions.
    The functions are arranged in hirachical order. Dependend on the need for reference_input and observations 
    you can implement your function in the approprate level in your derived class.
    Note: Some functions have to be implemented at least at the lowest level to get a working plant. **/


bool Plant::next_state(const double *state, const double *action, const double *reference_input, double *successor_state, double &reward)
{
  return next_state(state, action, successor_state,reward);
}

bool Plant::next_state(const double *state, const double *action, double *successor_state, double &reward)
{
  EOUT("Plant: No next_state function of plant was implemented. Please implement one of this this functions to get a system behavior.");
  return false;
}

void Plant::notify_command_string(char* buf){
  EOUT("Plant: No notify command behaviour.");
}

bool Plant::get_observation(const double *state, const double *reference_input, double *observation)
{
  return get_observation(state, observation);
}

bool Plant::get_observation(const double *state, double *observation)
{
  for (int i=0; i<state_dim; i++) {
    observation[i]=state[i];
  }
  return true;
}

bool Plant::check_initial_state(double *initial_state) 
{
  return true;
}


bool Plant::init_main(int& _state_dim, int& _observation_dim, int& _action_dim, int& _reference_input_dim, double& _delta_t, const char* fname)
{  
  bool res = init(_state_dim, _observation_dim, _action_dim, _reference_input_dim, _delta_t, fname); 
  state_dim = _state_dim;
  observation_dim = _observation_dim;
  return res;
}
bool Plant::init(int& _state_dim, int& _observation_dim, int& _action_dim, int& _reference_input_dim, double& _delta_t, const char* fname) { 
  _reference_input_dim = 0;
  return init(_state_dim, _observation_dim, _action_dim, _delta_t, fname);
}
bool Plant::init(int &_state_dim, int &_observation_dim, int &_action_dim, double &_delta_t, const char *fname) {
  bool res = init(_state_dim, _action_dim, _delta_t, fname);
  state_dim = _state_dim;
  observation_dim = state_dim;
  _observation_dim = state_dim;
  
  return res;
}
bool Plant::init(int &_state_dim, int &_action_dim, double &_delta_t, const char *fname)
{
  cout << "bin hier" << endl;
  read_options(fname);
  EOUT("Plant: No init function of plant was implemented. Please implement an init function to set all dimensions proper. More errors will occure due to zero dimension of vectors ... ");
  return false;
}


bool Plant::read_options(const char* fname) 
{
  char paramstr[MAX_STR_LEN];
  
  if(fname == 0)
    return true;

  /** to be modified according to the tasks of the environment. */
  ValueParser vp(fname,"Plant");
  if (vp.get("task",paramstr,MAX_STR_LEN) >= 0) {
    if (strcmp(paramstr,"Task 0") == 0)
      task_mode = 0;
    else if (strcmp(paramstr,"Task 1")== 0)
      task_mode = 1;
    else if (strcmp(paramstr,"Task 2")== 0)
      task_mode = 2;
    else {
       EOUT("Unknown task : (" << paramstr << ") using task 0");
       task_mode = 0;
    }
  }
  return true;
}





